<?php
	require("wwdb.php");
	require("waffle.php");
	function top5($playerCount) {
		$result = mysql_query("SELECT a.gameID,b.thisGameScore,c.playerName FROM gameStatus a JOIN scores b ON (a.gameID=b.gameID) JOIN players c ON (b.memberID=c.memberID) WHERE a.playerCount=$playerCount ORDER BY b.thisGameScore DESC LIMIT 5");
		echo "<table>";
		while ($i = mysql_fetch_assoc($result)) {
			echo "<tr><td><a href='leaderboard.php?username=" . $i["playerName"] . "'>" . $i["playerName"] . "</a></td>";
			echo "<td><a href='showScores.php?gameID=" . $i["gameID"] . "'>" . $i["thisGameScore"] . "</a></td></tr>";
		}
		echo "</table>";
	}
	
	$j = cleanparam("j");
	$username = cleanparam("username");
	membersdb();
	$result = mysql_query("SELECT * FROM members WHERE memberName='$username'");
	if ($row = mysql_fetch_assoc($result)) $memberID = $row["memberID"];
	else $username = null;

	wwdb();
	switch(true) {
	case $j=="userStats":
		if ($username != null) {
			echo "<table>";
			echo "<tr><th>Joined</th><td colspan=2>" . date("F j, Y g:i a", strtotime($row["joinDate"])) . "</td></tr>";
			echo "<tr><th>Last seen</th><td colspan=2>" . date("F j, Y g:i a", strtotime($row["lastLogin"])) . "</td></tr>";
			$result = mysql_query("SELECT * FROM scores WHERE memberID=$memberID");
			$gamesPlayed = mysql_num_rows($result);
			echo "<tr><th>Games played</th><td colspan=2>$gamesPlayed</td></tr>";
			$wins = mysql_query("SELECT gameStatus.playerCount, COUNT(gameStatus.gameID) AS played, SUM(scores.winner) AS wins FROM scores LEFT JOIN gameStatus ON scores.gameID = gameStatus.gameID WHERE scores.memberID = $memberID AND playerCount > 0 GROUP BY playerCount ORDER BY playerCount ASC");
			echo "</table><table><tr><th>Players</th><th>Games won</th><th>Games played</th></tr>";
			while ($i = mysql_fetch_assoc($wins))
				echo "<tr><td>" . $i["playerCount"] . "</td><td>" . $i["wins"] . "</td><td>" . $i["played"] . "</td></tr>";
			echo "</table>";
		}
		break;
	case preg_match("/highp[1-5]/",$j):
		top5($j[5]);
		break;
	case $j=="topWaffles":
		$result = mysql_query("SELECT * FROM scores ORDER BY thisGameScore DESC LIMIT 5");
		while ($row = mysql_fetch_assoc($result)) {
			drawWaffle($row["gameID"],$row["memberID"],true);
		}
		break;		
	case $j=="letterFrequency":
		$result = mysql_query("SELECT SUM(timesPicked) AS letterTotal FROM letterFrequency");
		$line = mysql_fetch_assoc($result);
		$letterCount = floatval($line["letterTotal"]);
		$result = mysql_query("SELECT timesPicked FROM letterFrequency ORDER BY timesPicked DESC LIMIT 1");
		$line = mysql_fetch_assoc($result);
		$highCount = floatval($line["timesPicked"]);
		$result = mysql_query("SELECT * FROM letterFrequency ORDER BY timesPicked DESC");
		echo "<table style='text-align:left'>";
		while ($line = mysql_fetch_assoc($result)) {
			$t = floatval($line["timesPicked"]);
			$pct = round(100*$t/$letterCount,1);
			$coeff = 100*$t/$highCount*.7;
			echo "<tr><td><div style='float:left;background-color:#0029A3;width:$coeff%;height:1em'></div>";
			echo "<span style='font-weight:bold'>&nbsp;" . $line["letter"] . "</span>&nbsp;-&nbsp;$pct%</td></tr>";
		}
		echo "</table>";
		break;
	}
	return;
?>